import { FormSchema } from '/@/components/Form';
import { isObject, cloneDeep } from 'lodash-es';
export const getSchemas = (nodeCfg: any): FormSchema[] => {
  // const { defaultConfiguration } = nodeCfg.configurationDescriptor.nodeDefinition;
  let list: Array<FormSchema> = [];
  switch (nodeCfg.name) {
    case 'device profile':
      list = [
        {
          field: 'persistAlarmRulesState',
          label: 'Persist state of alarm rules',
          component: 'Checkbox',
          defaultValue: false,
        },
        {
          field: 'fetchAlarmRulesStateOnStart',
          label: 'Fetch state of alarm rules',
          component: 'Checkbox',
          defaultValue: false,
        },
      ];
      break;
    case 'save attributes':
      list = [
        {
          field: 'scope',
          label: '设备属性范围',
          component: 'Select',
          componentProps: {
            allowClear: false,
            options: [
              { label: '客户端属性', value: 'CLIENT_SCOPE' },
              { label: '服务端属性', value: 'SERVER_SCOPE' },
              { label: '共享属性', value: 'SHARED_SCOPE' },
            ],
          },
          required: true,
          defaultValue: 'CLIENT_SCOPE',
        },
        {
          field: 'updateAttributesOnlyOnValueChange',
          label: 'Save attributes only if the value changes',
          component: 'Switch',
          defaultValue: false,
        },
        {
          field: 'sendAttributesUpdatedNotification',
          label: 'Send attributes updated notification',
          component: 'Switch',
          ifShow: ({ model }) => model.scope == 'SERVER_SCOPE' || model.scope == 'SHARED_SCOPE',
          defaultValue: false,
        },
        {
          field: 'notifyDevice',
          label: 'Notify device',
          component: 'Switch',
          ifShow: ({ model }) => model.scope == 'SHARED_SCOPE',
          defaultValue: false,
        },
      ];
      break;
    case 'save timeseries':
      list = [
        {
          field: 'defaultTTL',
          label: '默认TTL',
          component: 'InputNumber',
          componentProps: {
            placeholder: '请输入默认TTL',
          },
        },
        {
          field: 'skipLatestPersistence',
          label: 'Skip latest persistence',
          component: 'Checkbox',
          defaultValue: false,
        },
        {
          field: 'useServerTs',
          label: 'Use server ts',
          component: 'Checkbox',
          defaultValue: false,
        },
      ];
      break;
    case 'script':
      list = [
        {
          field: 'scriptLang',
          label: '',
          component: 'RadioButtonGroup',
          slot: 'scriptLangSlot',
          componentProps: {
            options: [
              {
                label: 'TBEL',
                value: 'TBEL',
              },
              {
                label: 'JavaScript',
                value: 'JS',
              },
            ],
          },
          colProps: {
            span: 24,
          },
          defaultValue: 'TBEL',
        },
        {
          field: 'tbelScript',
          label: '',
          component: 'Input',
          colProps: {
            span: 0,
          },
          defaultValue: 'return {msg: msg, metadata: metadata, msgType: msgType};',
        },
        {
          field: 'jsScript',
          label: '',
          component: 'Input',
          colProps: {
            span: 0,
          },
          defaultValue: 'return {msg: msg, metadata: metadata, msgType: msgType};',
        },
      ];
      break;
  }

  list.push({
    field: 'description',
    label: '描述',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入描述',
    },
    colProps: {
      span: 24,
    },
    defaultValue: '',
  });
  return [
    {
      field: 'name',
      label: '名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入名称',
      },
      required: true,
    },
    {
      field: 'debugMode',
      label: '调试模式',
      component: 'Switch',
      defaultValue: false,
    },
    ...list,
  ];
};

export const getNodeData = (data, descriptor, EntityType, nodeId, ruleChainId, isNewNode) => {
  const obj = {
    ...data,
    singletonMode: false,
    type: descriptor.value?.clazz || '',
    configuration:
      descriptor.value?.configurationDescriptor.nodeDefinition.defaultConfiguration || {},
    additionalInfo: { description: '', layoutX: 0, layoutY: 0 },
    id: { entityType: EntityType.RULE_NODE, id: nodeId.value },
    ruleChainId: ruleChainId.value,
    configurationVersion: descriptor.value?.configurationVersion || 0,
    createdTime: descriptor.value?.createdTime || new Date().getTime(),
    isNewNode: isNewNode.value,
  };
  return obj;
};

export const recursion = (data) => {
  let obj = {};
  const res = cloneDeep(data);
  Object.keys(res).forEach((key) => {
    if (isObject(res[key])) {
      obj = { ...obj, ...res[key] };
      recursion(res[key]);
    } else {
      obj[key] = res[key];
    }
  });

  return obj;
};

const NODE_TYPE = {
  FILTER: 'FILTER',
  ENRICHMENT: 'ENRICHMENT',
  TRANSFORMATION: 'TRANSFORMATION',
  ACTION: 'ACTION',
  EXTERNAL: 'EXTERNAL',
  FLOW: 'FLOW',
};

export const processingData = (key: string, data: any, descriptor?: any) => {
  const { name, type } = descriptor;
  if (key == 'display') {
    switch (name) {
      case 'script':
        return {
          name: data.name,
          debugMode: data.debugMode,
          description: data.description,
          transformScript: {
            scriptLang: data.scriptLang,
            jsScript: data.jsScript,
            tbelScript: data.tbelScript,
          },
        };
      default:
        return data;
    }
  } else if (key == 'save') {
    switch (name) {
      case 'script':
        return recursion(data);
      default:
        return data;
    }
  }
};
